Error: 'Crime Mapping/Week 1/downtown_homicides.csv' does not exist in current working directory ('/Users/mattashby/Documents/Crime Mapping Book/lectures').
# A tibble: 8 × 4
date latitude longitude description
<date> <dbl> <dbl> <chr>
1 2005-07-07 51.5 -0.0813 Suicide bomb exploded on train, killing 7
2 2005-07-07 51.5 -0.168 Suicide bomb exploded on train, killing 6
3 2005-07-07 51.5 -0.124 Suicide bomb exploded on train, killing 26
4 2005-07-07 51.5 -0.129 Suicide bomb exploded on bus, killing 13
5 2013-05-22 51.5 0.0623 Soldier fatally stabbed
6 2017-03-22 51.5 -0.122 Car driven at pedestrians and people stabbed, k…
7 2017-06-03 51.5 -0.0878 Van driven at pedestrians and people stabbed, k…
8 2017-06-19 51.6 -0.108 Van driven at pedestrians, killing 1
⚠️ Copy/paste with care: objects may not exist
# Load the R packages we need to analyse this datapacman::p_load(ggspatial, sf, tidyverse)# Download the data directly from a URL and store it as an objectattacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords =c("longitude", "latitude"), crs ="EPSG:4326")# Plot the mapggplot(homicides_sf_trans)
Error: object 'homicides_sf_trans' not found
⚠️ Copy/paste with care: different maps need different choices
# Transform the data to a co-ordinate reference system for the state of Georgiafatal_terrorist_sf_trans <-st_transform(fatal_terrorist_sf, "EPSG:26967")
⚠️ Copy/paste with care: different maps need different choices
# Transform the data to a co-ordinate reference system for the city of Londonfatal_terrorist_sf_trans <-st_transform(fatal_terrorist_sf, "EPSG:26967")
😎 Copy/paste with care: different maps need different choices
# Transform the data to a co-ordinate reference system for the UKfatal_terrorist_sf_trans <-st_transform(fatal_terrorist_sf, "EPSG:27700")
🚫 Don’t use zoom in annotation_map_tile()
# Load the R packages we need to analyse this datapacman::p_load(ggspatial, sf, tidyverse)# Download the data directly from a URL and store it as an objectattacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords =c("longitude", "latitude"), crs ="EPSG:4326")# Plot the mapggplot(attacks) +annotation_map_tile(zoom =0, progress ="none") +geom_sf(size =4)
😎 Use zoomin in annotation_map_tile()
# Load the R packages we need to analyse this datapacman::p_load(ggspatial, sf, tidyverse)# Download the data directly from a URL and store it as an objectattacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords =c("longitude", "latitude"), crs ="EPSG:4326")# Plot the mapggplot(attacks) +annotation_map_tile(zoomin =0, progress ="none") +geom_sf(size =4)
🚫 Don’t forget to comment your code
pacman::p_load(ggspatial, sf, tidyverse)attacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords =c("longitude", "latitude"), crs ="EPSG:4326")ggplot(attacks) +annotation_map_tile(zoomin =0, progress ="none") +geom_sf(size =4) +scale_x_continuous(expand =expansion(mult =0.5)) +scale_y_continuous(expand =expansion(mult =0.2)) +labs(title ="Fatal Terrorist Attacks in London Between 2010 and 2018.",caption ="Background map by OpenStreetMap" ) +theme_void()
😎 Comment your code
# Load the R packages we need to analyse this datapacman::p_load(ggspatial, sf, tidyverse)# Download the data directly from a URL and store it as an objectattacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords =c("longitude", "latitude"), crs ="EPSG:4326")# Plot the mapggplot(attacks) +annotation_map_tile(zoomin =0, progress ="none") +geom_sf(size =4) +scale_x_continuous(expand =expansion(mult =0.5)) +scale_y_continuous(expand =expansion(mult =0.2)) +labs(title ="Fatal Terrorist Attacks in London Between 2010 and 2018.",caption ="Background map by OpenStreetMap" ) +theme_void()
Comments start with #and a space
🚫 Incorrect:
#This is a comment
😎 Correct:
# This is a comment
This is going to matter a lot later in the module
🚫 Don’t compress your code
# Load the R packages we need to analyse this datapacman::p_load(ggspatial,sf,tidyverse)# Download the data directly from a URL and store it as an objectattacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords=c("longitude","latitude"), crs="EPSG:4326")# Plot the mapggplot(attacks) +annotation_map_tile(zoomin=0,progress="none") +geom_sf(size=4) +scale_x_continuous(expand=expansion(mult=0.5)) +scale_y_continuous(expand=expansion(mult=0.2)) +labs(title="Fatal Terrorist Attacks in London Between 2010 and 2018.",caption="Background map by OpenStreetMap" ) +theme_void()
🌬️ Let your code breathe
# Load the R packages we need to analyse this datapacman::p_load(ggspatial, sf, tidyverse)# Download the data directly from a URL and store it as an objectattacks <-read_csv("https://mpjashby.github.io/crimemappingdata/london_attacks.csv") |>st_as_sf(coords =c("longitude", "latitude"), crs ="EPSG:4326")# Plot the mapggplot(attacks) +annotation_map_tile(zoomin =0, progress ="none") +geom_sf(size =4) +scale_x_continuous(expand =expansion(mult =0.5)) +scale_y_continuous(expand =expansion(mult =0.2)) +labs(title ="Fatal Terrorist Attacks in London Between 2010 and 2018.",caption ="Background map by OpenStreetMap" ) +theme_void()
The pipe operator |>
Lots of R code looks like this:
# This script counts the number of robberies on each day of the week in the# first quarter of 2019# Load robbery datarobbery1 <-read_csv("https://mpjashby.github.io/crimemappingdata/san_francisco_robbery.csv")# Select only the columns we needrobbery2 <-select(robbery1, date_time)# Filter only those offences that occurred in the first quarter of 2019robbery3 <-filter(robbery2, as_date(date_time) <=ymd("2019-03-31"))# Create a new weekday variablerobbery4 <-mutate(robbery3, weekday =wday(date_time, label =TRUE))# Count how many offences occurred on each weekdaycount(robbery4, weekday)
Lots of R code looks like this:
# This script counts the number of robberies on each day of the week in the# first quarter of 2019# Load robbery datarobbery1 <-read_csv("https://mpjashby.github.io/crimemappingdata/san_francisco_robbery.csv")# Select only the columns we needrobbery2 <-select(robbery1, date_time)# Filter only those offences that occurred in the first quarter of 2019robbery3 <-filter(robbery2, as_date(date_time) <=ymd("2019-03-31"))# Create a new weekday variablerobbery4 <-mutate(robbery3, weekday =wday(date_time, label =TRUE))# Count how many offences occurred on each weekdaycount(robbery4, weekday)
robbery1 is (a) created, (b) used once, and (c) never used again
Lots of R code looks like this:
# This script counts the number of robberies on each day of the week in the# first quarter of 2019# Load robbery datarobbery1 <-read_csv("https://mpjashby.github.io/crimemappingdata/san_francisco_robbery.csv")# Select only the columns we needrobbery2 <-select(robbery1, date_time)# Filter only those offences that occurred in the first quarter of 2019robbery3 <-filter(robbery2, as_date(date_time) <=ymd("2019-03-31"))# Create a new weekday variablerobbery4 <-mutate(robbery3, weekday =wday(date_time, label =TRUE))# Count how many offences occurred on each weekdaycount(robbery4, weekday)
The same is true with robbery2, robbery3 and robbery4
create + use once + never use again ▶️ no need to create object
Unnecessary objects:
make code longer ▶️ harder to read ▶️ harder to understand
😎 Comment your code